/* eslint-disable prettier/prettier */
import { create } from 'dva-core';
import createLoading from 'dva-loading';
import modelExtend from 'dva-model-extend';
import Toast from 'react-native-root-toast';
import models from '@/models/index';
import homeModel from '@/models/home';

// 1.创建实例
const app = create({
    onError: (e) => {
        Toast.show('网络异常！', {
            position: Toast.positions.CENTER,
            duration: Toast.durations.LONG,
            shadow: true,
            animation: true,
        });
    },
});
// 2.加载model对象
models.forEach((model) => {
    app.model(model);
});
app.use(createLoading());
// 3.启动dva
app.start();
// 4.导出dva的数据
export default app._store;

const cached = {
    home: true,
};

function registerModel(model) {
    if (!cached[model.namespace]) {
        app.model(model);
        cached[model.namespace] = true;
    }
}

export function createHomeModel(namespace) {
    const model = modelExtend(homeModel, { namespace });
    registerModel(model);
}
